package com.mf.controller;

import com.mf.entity.Task;
import com.mf.entity.TaskDetail;
import com.mf.entity.TaskDto;
import com.mf.entity.TaskVo;
import com.mf.service.impl.TaskDetailServiceImpl;
import com.mf.service.impl.TaskServiceImpl;
import com.mf.utils.Result;
import com.mf.verify.verifyField;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 任务 前端控制器
 * </p>
 *
 * @author mf
 * @since 2024-04-13
 */
@RestController
@RequestMapping("/task")
@Tag(name = "任务")
public class TaskController {

    @Resource
    private TaskServiceImpl taskService;

    @Resource
    private TaskDetailServiceImpl taskDetailService;

    /**
     * 新增任务
     * @param task 待新增的任务对象
     * @return 返回任务新增结果，成功为true，失败为false
     */
    @PostMapping("/add")
    @Operation(summary = "新增任务")
    public Result<Boolean> add(@RequestBody Task task) {
        verifyField.isEmpty(task, List.of("tName","type","cycleType"));

        // 判断任务名称是否重复
        taskService.getByName(task.getTName());
        return Result.success(taskService.save(task));
    }

    /**
     * 修改任务
     * @param task 待修改的任务对象
     * @return 返回任务修改结果，成功为true，失败为false
     */
    @PostMapping("/update")
    @Operation(summary = "修改任务")
    public Result<Boolean> update(@RequestBody Task task) {
        return Result.success(taskService.updateById(task));
    }

    /**
     * 根据ID删除任务
     * @param id 待删除任务的ID
     * @return 返回任务删除结果，成功为true，失败为false
     */
    @GetMapping("/delete")
    @Operation(summary = "删除任务")
    public Result<Boolean> delete(@RequestParam("id") Integer id) {
        return Result.success(taskService.removeById(id));
    }

    /**
     * 批量删除任务
     * @param ids 待删除任务的ID列表
     * @return 返回任务批量删除结果，成功为true，失败为false
     */
    @PostMapping("/deleteBatch")
    @Operation(summary ="批量删除")
    public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(taskService.removeByIds(ids));
    }

    /**
     * 根据ID查询任务
     * @param id 待查询任务的ID
     * @return 返回查询到的任务对象，如果不存在则返回null
     */
    @GetMapping("/findById")
    @Operation(summary = "根据id查询")
    public Result<Task> findById(@RequestParam("id") Integer id) {
        return Result.success(taskService.getById(id));
    }

    /**
     * 查询所有任务
     * @return 返回任务列表
     */
    @PostMapping("/findAll")
    @Operation(summary = "查询所有")
    public Result<List<Task>> findAll(@RequestBody Task task) {
        return Result.success(taskService.findAll(task));
    }

    @PostMapping("/findByDate")
    @Operation(summary = "1-根据日期查询")
    public Result<List<TaskVo>> findByDate() {
        final Task task = new Task();
        task.setStatus(0);
        final List<Task> taskList = taskService.findAll(task);
        final ArrayList<Integer> arrayList = new ArrayList<>();
        for (Task task1 : taskList) {
            arrayList.add(task1.getId());
        }

        final List<TaskDetail> detailList = taskDetailService.findByTIds(arrayList);

        final ArrayList<TaskVo> taskVos = new ArrayList<>();

        for (Task task2 : taskList) {
            final TaskVo taskVo = new TaskVo();
            BeanUtils.copyProperties(task2, taskVo);
            for (TaskDetail list1 : detailList) {
                if(task2.getId().equals(list1.getTId()) ){
                    taskVo.setStartTime(list1.getStartTime());
                    taskVo.setEndTime(list1.getEndTime());
                    taskVo.setDetailId(list1.getId());
                }
            }
            taskVos.add(taskVo);
        }

        return Result.success(taskVos);
    }
}
